package patrol.mobile.com.mobilepatrol.persenter;

import android.os.Handler;
import android.os.Message;

import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.api.ByteCallback;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.utils.Compress;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.utils.ThreadManager;
import patrol.mobile.com.mobilepatrol.view.RoadDataView;

/**
 * Created by apple on 17/9/27.
 */

public class RoadDataPer {
    private RoadDataView mView;
    int page = 0;
    String orgNo;
    int total;
    final List<String> data = new ArrayList<String>();
//    final List<RouteSurveyTaskDataBO> data = new ArrayList<RouteSurveyTaskDataBO>();
    public RoadDataPer(RoadDataView mView, String orgNo,int total) {
        this.mView = mView;
        this.orgNo = orgNo;
        this.total = total;
    }
    public void getData() {
        page++;
        OkHttpUtils
                .post()
                .addParams("type","surveyData")
                .addParams("pageNo", String.valueOf(page))
                .addParams("pageSize", String.valueOf(5000))
                .addParams("orgNo",orgNo)
                .url(SPUtils.get(Constants.current_ser,"") + Urls.initData)
                .build()
                .execute(new ByteCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mView.hideLoading();
                        mView.onError(e.getMessage());
                    }

                    @Override
                    public void onResponse(final byte[] response, int id) {
                        final Handler handler = new Handler(){
                            @Override
                            public void handleMessage(Message msg) {
//                                final JSONArray value = (JSONArray) msg.obj;

//                                PatrolApp.realm.executeTransactionAsync(new Realm.Transaction() {
//                                    @Override
//                                    public void execute(Realm realm) {
//                                        realm.createAllFromJson(RouteSurveyTaskDataBO.class, value.toString());
//                                    }
//                                });
                                if (msg.what==1){
                                    if (total==page){
                                        mView.hideLoading();
//                                        RoadSurveyManager.insertRouteSurveyTaskDataBOs(data);
                                        mView.onDataSuccess(data);
                                    }else{
                                        mView.onPage(page);
                                        getData();
                                    }
                                }

                            }
                        };
                        ThreadManager.getNormalPool().execute(new Runnable() {
                            @Override
                            public void run() {
                                String result = null;
                                try {
                                    result = Compress.decompressByteArray(response);
                                    try {
                                        JSONObject object = new JSONObject(result);
                                        if (object.getInt("code")==200){
                                            final JSONArray value = object.getJSONArray("value");
//                                            List<RouteSurveyTaskDataBO> mList = GsonUtil.jsonToList(value.toString(),RouteSurveyTaskDataBO.class);
//                                            data.addAll(mList);
                                            data.add(value.toString());
                                            Message msg = handler.obtainMessage();
                                            msg.what = 1;
                                            handler.sendMessage(msg);
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

                            }
                        });

//                        try {
//                            String result = Compress.decompressByteArray(response);
//                            JSONObject object = new JSONObject(result);
//                        if (object.getInt("code")==200){
//                            final JSONArray value = object.getJSONArray("value");
////                            List<RouteSurveyTaskDataBO> v = GsonUtil.jsonToList(value.toString(),RouteSurveyTaskDataBO.class);
////                            mList.addAll(v);
//                            PatrolApp.realm.executeTransactionAsync(new Realm.Transaction() {
//                                @Override
//                                public void execute(Realm realm) {
//                                    realm.createAllFromJson(RouteSurveyTaskDataBO.class, value.toString());
//                                }
//                            });
//                            if (total==page){
//                                mView.hideLoading();
//                                mView.onDataSuccess("over");
////                                mView.saveData(value.toString());
//                            }else{
//                                getData();
////                                mView.saveData(value.toString());
//                            }
//                        }else{
//                            mView.onError("初始化失败");
//                        }
//
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//
                    }
                });
    }
}
